<template>
  <div class="login">
    <van-nav-bar title="面经登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          square
          block
          type="info"
          native-type="submit"
          color="#fa6d1d"
          >提交</van-button
        >
      </div>
    </van-form>
    <div class="login-button">
      <router-link to="/register" class="ttt">注册账号</router-link>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";
import { auth } from "@/utils/Auth";

export default {
  name: "HmLogin",
  data() {
    return {
      username: "",
      password: "",
      visible: false,
    };
  },
  methods: {
    async onSubmit(values) {
      if (!values.username.trim()) {
        return;
      }
      if (!values.password.trim()) {
        return;
      }
      login(values)
        .then((res) => {
          // console.log(res);
          console.log(res.data.data.token);
          auth.saveUser(res.data.data.token);
          this.$router.push("/");
        })
        .catch(() => {
          this.$toast.fail("用户名或密码错误");
        });
    },
  },
};
</script>

<style scoped lang="less">
.login-button {
  margin-top: 20px;
  width: 90%;
  margin-left: 5%;
}
.login-button {
  display: flex;
  justify-content: end;
  .ttt {
    width: 60px;
    margin-right: 5%;
    text-align: center;
    color: #069;
    font-size: 12px;
  }
}
</style>
